package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTheme;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.StrategyQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyThemeService strategyThemeService;
    @Reference
    private IStrategyContentService strategyContentService;
    @Reference
    private IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyQuery qo, Model model){
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategyDetail/list";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null){
            StrategyDetail strategyDetail = strategyDetailService.get(id);
            //查询内容
            StrategyContent content = strategyContentService.get(id);
            strategyDetail.setStrategyContent(content);
            model.addAttribute("entity",strategyDetail);

            //查询标签
            String tags = strategyTagService.getNameByDetailId(id);
            model.addAttribute("tags",tags);
        }
        //获取所有分类
        List<StrategyCatalog> catalogs = strategyCatalogService.selectAll();
        model.addAttribute("catalogs",catalogs);
        //获取所有主题
        List<StrategyTheme> themes = strategyThemeService.selectAll();
        model.addAttribute("themes",themes);
        return "strategyDetail/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail,String tags){
        AjaxResult result = new AjaxResult();
        try {
            strategyDetailService.saveOrUpdate(strategyDetail,tags);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
